package com.demo.OtherTest;

import org.apache.commons.lang3.time.DateUtils;

import java.math.BigDecimal;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Test {
    public static void main(String[] args) throws ParseException {
        /*SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        List<String> quaList = new ArrayList();
        List<String> monthList = new ArrayList();
        List<String> dayList = new ArrayList();
        List<String> weekList = new ArrayList();

        List<String> timeList = new ArrayList<>();
        String startTime = "20220701";
        String endTime = "20220831";
        quaList.add(startTime + "_" + startTime);
        getQuaInfo(quaList,endTime);
        quaList.remove(0);
        System.out.println(quaList.toString());

        timeList.add(startTime + "_" + startTime);
        getMonthInfo(timeList,monthList,endTime);
        System.out.println(monthList.toString());

        dayList = findEveryDay(startTime, endTime);
        System.out.println(dayList.toString());

      *//*  String qua = getQua(startTime);
        System.out.println(qua);*//*

        weekList.add(startTime + "_" + startTime);
        getWeekInfo(weekList, endTime);
        System.out.println(weekList.toString());*/
//        System.out.println(new Date(System.currentTimeMillis()));
//        System.out.println(daysBetween1("2022-11-02"));
       /* List<String> list = new ArrayList<>();

        List<String> aaa = new ArrayList<>();
        aaa.add("111");
        aaa.add("222");
        aaa.add("333");
        aaa.add("444");
        list.addAll(aaa);
        System.out.println(list.toString());*/
        double distance = getDistance(0.0, 0.0, 30.32126, 120.073717);
        System.out.println(distance);
    }


    private static double getDistance(Double lat1, Double lng1, Double lat2, Double lng2) {
        // 存在null值，无法计算，返回 NULL_DISTANCE 99999.99
        if (null == lat1 || null == lng1 || null == lat2 || null == lng2) {
            return 0.00;
        }
        double radLat1 = getRadian(lat1);
        double radLat2 = getRadian(lat2);
        double a = radLat1 - radLat2;// 两点纬度差
        double b = getRadian(lng1) - getRadian(lng2);// 两点的经度差
        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1)* Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));

        return new BigDecimal(distance * 6378.137).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    private static double getRadian(double degree) {
        return degree * Math.PI / 180.0;
    }


    public static void getWeekInfo(List<String> weekList,String endTime) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String time = "";
        String s = weekList.get(weekList.size() - 1);
        String[] split = s.split("_");
        int dayNum = 0;
        Calendar calendar = Calendar.getInstance();
        if (weekList.size() <= 1){
            time  = split[1];
            Date date = sf.parse(time);
            calendar.setTime(date);
            int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (0 == day){
                dayNum  = 7 - 1;
            }else {
                dayNum = day - 1;
            }
            calendar.add(Calendar.DATE, - dayNum);
        }else {
            Date date = sf.parse(split[1]);
            calendar.setTime(date);
            calendar.add(Calendar.DATE,1);
        }
        String weekFirst = sf.format(calendar.getTime());
        calendar.add(Calendar.DATE, 6);
        String weekLast = sf.format(calendar.getTime());
        weekList.add(weekFirst + "_" + weekLast);
        if (Integer.valueOf(weekLast) < Integer.valueOf(endTime)){
            getWeekInfo(weekList,endTime);
        }
    }





    public static List<String> findEveryDay(String beginTime, String endTime)
            throws ParseException {
        //1.创建一个放所有日期的集合
        List<String> dates = new ArrayList();
        //2.创建时间解析对象规定解析格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        //3.将传入的时间解析成Date类型,相当于格式化
        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);
        //4.将格式化后的第一天添加进集合
        dates.add(sdf.format(dBegin));
        //5.使用本地的时区和区域获取日历
        Calendar calBegin = Calendar.getInstance();
        //6.传入起始时间将此日历设置为起始日历
        calBegin.setTime(dBegin);
        //8.判断结束日期是否在起始日历的日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 9.根据日历的规则:月份中的每一天，为起始日历加一天
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            //10.得到的每一天就添加进集合
            dates.add(sdf.format(calBegin.getTime()));
            //11.如果当前的起始日历超过结束日期后,就结束循环
        }
        return dates;
    }


    //计算指定范围内每季度开始_结束时间
    public static void getMonthInfo(List<String> timeList,List<String> monthList, String endTime) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String time = "";
        String s = timeList.get(timeList.size() - 1);
        String[] split = s.split("_");
        if (timeList.size() <= 1){
            time  = split[1];
        }else {
            Date date = sf.parse(split[1]);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            ca.add(Calendar.DATE,1);
            time = sf.format(ca.getTime());
        }
        Date date = sf.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        String firstDay = sf.format(calendar.getTime());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDay = sf.format(calendar.getTime());
        timeList.add(firstDay + "_" + lastDay);
        monthList.add(firstDay.substring(0,6));
        if (Integer.valueOf(lastDay) < Integer.valueOf(endTime)){
            getMonthInfo(timeList,monthList,endTime);
        }
    }



    //计算指定范围内每季度开始_结束时间
    public static void getQuaInfo(List<String> quaList, String endTime) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        String time = "";
        String s = quaList.get(quaList.size() - 1);
        String[] split = s.split("_");
        if (quaList.size() <= 1){
            time  = split[1];
        }else {
            Date date = sf.parse(split[1]);
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            ca.add(Calendar.DATE,1);
            time = sf.format(ca.getTime());
        }
        int year = Integer.valueOf(time.substring(0,4));
        Date date = sf.parse(time);
        //获取季度数
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int quarter = calendar.get(Calendar.MONTH) / 3 + 1;

        int startMonth = (quarter - 1) * 3;

        // 根据月获取开始时间
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, startMonth);
        cal.set(Calendar.DAY_OF_MONTH, 1);

        String firstDay = sf.format(cal.getTime());

        //该季最后一天
        int lastMonth = quarter * 3 - 1;
        cal.set(Calendar.MONTH, lastMonth);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDay = sf.format(cal.getTime());
        quaList.add(firstDay + "_" + lastDay);
        if (Integer.valueOf(lastDay) < Integer.valueOf(endTime)){
            getQuaInfo(quaList,endTime);
        }
    }

    public static String getQua(String time) throws ParseException {
        String quarter="";
        String substring = time.substring(4, 6);
        int m = Integer.valueOf(substring);
        if (m >= 1 && m == 3) {
            quarter = "1";
        }
        else if (m >= 4 && m <= 6) {
            quarter = "2";
        }
        else if (m >= 7 && m <= 9) {
            quarter = "3";
        }
        else {
            quarter = "4";
        }
        return quarter;
    }



    public static String daysBetween1(String birthdayString) throws ParseException {
        if (birthdayString == null) {
            return "";
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDate = format.parse(birthdayString);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(birthDate);
        calendar.add(Calendar.DATE,7);
        long endTime = calendar.getTimeInMillis();
        calendar.setTime(new Date());
        long startTime = calendar.getTimeInMillis();
        long days=(endTime-startTime)/(1000*3600*24);
        int day = Integer.parseInt(String.valueOf(days));
        return String.valueOf(day);
    }
}
